package qc.module.platform.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import qc.common.core.exception.QCPromptException;
import qc.common.core.tree.TreeDataDto;
import qc.common.core.unify.QCUnifyReturnValue;
import qc.module.platform.dto.dept.DeptQueryConditionDto;
import qc.module.platform.dto.user.*;
import qc.module.platform.service.UserService;

import java.util.List;

/**
 * UserController
 *
 * @author QuCheng Tech
 * @create 2023/1/9
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    //是否为逻辑删除，为true表示逻辑删除--修改记录标记，为false表示物理删除--从数据库表中删除记录
    private static final boolean isLogicDelete = true;

    /**
     * 根据关键字查询，关键字可以是名称、登录用户名、手机号、微信id，没有则查询全部
     *
     * @param dto UserQueryConditionDto
     * @return 用户信息集合
     * @author QcCheng Tech
     * @since 2023/2/8
     */
    @RequestMapping(value = "/queryplatform", method = {RequestMethod.GET, RequestMethod.POST})
    public List<UserInfoDto> queryPlatformUser(@RequestBody UserQueryConditionDto dto) {
        return userService.queryPlatformUser(dto, null);
    }

    /**
     * 返回简要信息不对用户状态进行判断
     *
     * @param dto UserQueryConditionDto
     * @return 用户简要信息集合
     * @author QcCheng Tech
     * @since 2023/2/8
     */
    @RequestMapping(value = "/queryplatformsimple", method = {RequestMethod.GET, RequestMethod.POST})
    public List<UserSimpleInfoDto> queryPlatformUserSimple(@RequestBody UserQueryConditionDto dto) {
        return userService.queryPlatformUserSimple(dto, null);
    }

    /**
     * 查询项目用户
     * 根据关键字查询，关键字可以是名称、登录用户名、手机号、微信id，没有则查询全部
     *
     * @param dto UserQueryConditionDto
     * @return 用户信息集合
     * @author QcCheng Tech
     * @since 2023/2/8
     */
    @RequestMapping(value = "/queryproject", method = {RequestMethod.GET, RequestMethod.POST})
    public List<UserInfoDto> queryProjectUsers(@RequestBody UserQueryConditionDto dto) throws QCPromptException {
        return userService.queryProjectUsers(dto);
    }

    /**
     * 查询用户项目信息
     * 根据关键字查询，关键字可以是名称、登录用户名、手机号、微信id，没有则查询全部
     *
     * @param dto UserQueryConditionDto
     * @return 用户简要信息集合
     * @author QcCheng Tech
     * @since 2023/2/8
     */
    @RequestMapping(value = "/queryprojectsimple", method = {RequestMethod.GET, RequestMethod.POST})
    public List<UserSimpleInfoDto> queryProjectUserSimple(@RequestBody UserQueryConditionDto dto) throws QCPromptException {
        return userService.queryProjectUserSimple(dto);
    }

    /**
     * 新增项目用户
     *
     * @param dto 新增项目用户
     * @return 成功返回null，失败返回错误信息
     * @author QuCheng
     */
    @RequestMapping(value = "/addproject", method = {RequestMethod.GET, RequestMethod.POST})
    public String addProjectUser(@RequestBody UserAddDto dto) throws QCPromptException {
        return userService.add(dto);
    }

    /**
     * 新增平台用户
     *
     * @param dto 新增用户信息
     * @return 成功返回null，失败返回错误信息
     * @author QuCheng
     */
    @RequestMapping(value = "/addplatform", method = {RequestMethod.GET, RequestMethod.POST})
    public String addPlatformUser(@RequestBody UserAddDto dto) throws QCPromptException {
        return userService.add(dto);
    }

    /**
     * 修改用户
     *
     * @param dto 用户信息
     * @return 成功返回null，失败返回错误信息
     * @author QuCheng
     */
    @RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.PUT})
    public String update(@RequestBody UserEditDto dto) throws QCPromptException {
        return userService.update(dto);
    }

    /**
     * 删除平台用户
     *
     * @param id 用户ID
     * @return 成功返回null，失败返回错误信息
     * @author QuCheng
     */
    @RequestMapping(value = "/deleteplatform", method = {RequestMethod.DELETE})
    public String deletePlatformUserByParam(@RequestParam int id) {
        return userService.delete(id, isLogicDelete);
    }

    /**
     * 删除平台用户
     *
     * @param id 用户ID
     * @return 成功返回null，失败返回错误信息
     * @author QuCheng
     */
    @RequestMapping(value = "/deleteplatform/{id]", method = {RequestMethod.DELETE})
    public String deletePlatformUserByPath(@PathVariable(required = true) int id) {
        return userService.delete(id, isLogicDelete);
    }

    /**
     * 删除项目用户关联关系
     *
     * @param
     * @return 成功返回null，失败返回错误信息
     * @author QuCheng
     */
    @RequestMapping(value = "/deleteproject", method = {RequestMethod.DELETE, RequestMethod.POST})
    public String deleteProjectUser(@RequestBody UserDeleteDto dto) {
        if (dto == null)
            return QCUnifyReturnValue.Warn("删除的对象不能为空");

        return userService.deleteProjectUser(dto.getId(), dto.getProject(), isLogicDelete);
    }

    /**
     * 禁用用户
     *
     * @param id 用户id
     * @return 成功返回null，失败返回错误信息
     */
    @RequestMapping(value = "/disable", method = {RequestMethod.POST})
    public String disableByParam(@RequestParam Integer id) {
        return userService.disable(id);
    }

    /**
     * 禁用用户
     *
     * @param id 用户id
     * @return 成功返回null，失败返回错误信息
     */
    @RequestMapping(value = "/disable/{id}", method = {RequestMethod.POST})
    public String disableByPath(@PathVariable Integer id) {
        return userService.disable(id);
    }

    /**
     * 启用用户
     *
     * @param id 用户id
     * @return 成功返回null，失败返回错误信息
     */
    @RequestMapping(value = "/enable", method = {RequestMethod.POST})
    public String enableByParam(@RequestParam Integer id) {
        return userService.enable(id);
    }

    /**
     * 启用用户
     *
     * @param id 用户id
     * @return 成功返回null，失败返回错误信息
     */
    @RequestMapping(value = "/enable/{id}", method = {RequestMethod.POST})
    public String enableByPath(@PathVariable Integer id) {
        return userService.enable(id);
    }

    /**
     * 获取指定用户信息，从param参数中传值
     *
     * @param id 用户ID
     * @return 用户信息
     * @author QuCheng
     */
    @RequestMapping(value = "/get", method = {RequestMethod.GET})
    public UsersDto getByParam(@RequestParam int id) throws QCPromptException {
        return userService.get(id);
    }

    /**
     * 获取指定用户信息，从url中传值
     *
     * @param id 用户ID
     * @return 用户信息
     * @author QuCheng
     */
    @RequestMapping(value = "/get/{id}", method = {RequestMethod.GET})
    public UsersDto getByPath(@PathVariable(required = true) int id) throws QCPromptException {
        return userService.get(id);
    }

    /**
     * 重置密码
     *
     * @param id 用户ID
     * @return 成功返回null
     * @author QuCheng
     */
    @RequestMapping(value = "/resetpwd", method = {RequestMethod.GET, RequestMethod.POST})
    public UserResetPasswordResultDto resetPasswordByParam(@RequestParam int id) {
        return userService.resetPassword(id, true);
    }

    /**
     * 重置密码
     *
     * @param id 用户Id
     * @return 成功返回null
     * @throws QCPromptException 异常处理
     */
    @RequestMapping(value = "/resetpwd/{id}", method = {RequestMethod.GET, RequestMethod.POST})
    public UserResetPasswordResultDto resetPasswordByPath(@PathVariable(required = true) int id) {
        return userService.resetPassword(id, true);
    }

    /**
     * 获取用户项目信息
     *
     * @param id 用户id
     * @return 用户项目信息````
     * @author QuCheng
     */
    @RequestMapping(value = "/getproject/{id}", method = {RequestMethod.GET})
    public List<UserProjectDto> getUserProjectsByPath(@PathVariable() int id) throws QCPromptException {
        return userService.getUserProjects(id);
    }

    /**
     * 获取用户项目信息
     *
     * @param id 用户id
     * @return 用户项目信息
     * @author QuCheng
     */
    @RequestMapping(value = "/getproject", method = {RequestMethod.GET})
    public List<UserProjectDto> getUserProjectsByParam(@RequestParam int id) throws QCPromptException {
        return userService.getUserProjects(id);
    }

    /**
     * 设置用户与项目的关联关系
     *
     * @param dto 用户项目关联关系
     * @return 成功返回null
     */
    @RequestMapping(value = "/setprojects", method = {RequestMethod.POST})
    public String setUserProjects(@RequestBody UserProjectSetDto dto) {
        return userService.setUserProjects(dto);
    }

    /**
     * 查询用户部门关系
     *
     * @param dto 用户部门关联关系
     * @return 成功返回null
     * @throws QCPromptException
     */
    @RequestMapping(value = "/getdepts", method = {RequestMethod.GET, RequestMethod.POST})
    public List<UserDeptDto> getUserDepts(@RequestBody DeptQueryConditionDto dto) throws QCPromptException {
        return userService.getUserDepts(dto, false);
    }

    /**
     * 设置用户与部门的关联关系
     *
     * @param dto 用户部门关联关系
     * @return 成功返回null
     */
    @RequestMapping(value = "/setdepts", method = {RequestMethod.GET, RequestMethod.POST})
    public String setUserDepts(@RequestBody UserDeptSetDto dto) {
        return userService.setUserDepts(dto);
    }

    /**
     * 批量导入用户信息
     *
     * @param dto 文件信息
     * @return 成功返回null
     */
    @RequestMapping(value = "/import", method = {RequestMethod.GET, RequestMethod.POST})
    public String importUsers(@RequestBody UserImportDto dto) {
        return userService.importUsers(dto);
    }

    /**
     * 以树形结构返回结果，按部门的上下级关系列出所有部门；
     *
     * @param deptsConditionDto:条件
     * @return TreeDataDto 返回结果集
     * @author QcCheng Tech
     * @date 2023/9/8
     */
    @RequestMapping(value = "/getdeptstree", method = {RequestMethod.GET, RequestMethod.POST})
    public TreeDataDto getUserDeptsTree(@RequestBody UserDeptsConditionDto deptsConditionDto) throws QCPromptException {
        return userService.getUserDeptsTree(deptsConditionDto);
    }

}
